/*
 * @Description:
 * @Author: maxyang
 * @Date: 2022-05-19 10:02:48
 * @LastEditTime: 2022-08-09 22:17:17
 * @LastEditors: liutq
 * @Reference:
 */
package service

import (
	"encoding/json"
	"fmt"
	"strconv"
	"strings"
	"time"

	"github.com/maxyang107/jinglan/common"
	"github.com/maxyang107/jinglan/model"
	"github.com/maxyang107/jinglan/model/goods"
	"github.com/maxyang107/jinglan/model/system"
	"github.com/maxyang107/jinglan/model/user"
)

var (
	GoodsService = &GoodsSer{}
)

type GoodsSer struct{}

func (g GoodsSer) GetGoodsList(query_ common.GoodsList) goods.GoodsList {
	if query_.PerPage == 0 {
		query_.PerPage = 10
	}

	return goods.GetGoodsList(query_)
}

func (g GoodsSer) GetGoodsDetail(id_ uint, userid uint) goods.GoodsTmp {
	gt := goods.GetGoodsDetail(int(id_))
	if gt.GoodsCate == 3 {
		dr := goods.GetDrawRecordByUidAndGid(userid, id_)
		if dr.ID == 0 {
			gt.VshopUrl = ""
		} else {
			extime := gt.Drawtime.Unix() + 86400

			if extime < time.Now().Unix() {
				gt.VshopUrl = ""
			}
		}
		i := user.GetDrawCollect(id_, userid)
		if i > 0 {
			gt.VshopUrl = ""
		}

	}
	return gt
}

func (g GoodsSer) GetGoodsListForDraw(query_ common.GoodsList) goods.GoodsList1 {
	if query_.PerPage == 0 {
		query_.PerPage = 10
	}

	return goods.GetGoodsListForDraw(query_)
}

func (g GoodsSer) Synchronize() {
	orders, err := common.GetVOrderList()
	var orderId []string
	if err != nil {
		fmt.Println(err.Error())
	}
	if orders.StatusInfo.StatusCode == 0 {
		for _, v := range orders.Result.Orders {
			comma := strings.Index(v.Img, "?")
			img := v.Img[:comma] + "?w=480&h=480&cp=1"
			//检查商品是否需要限制购买
			g2 := goods.GetGoodsByImgUrl(img)
			if g2.ID == 0 {
				continue
			}

			//检查是否有该用户
			phone, err := strconv.ParseInt(v.Buyer.Phone, 10, 64)
			if err != nil {
				fmt.Println(err.Error())
				continue
			}
			u := user.GetUserByPhone(phone)
			if u.ID == 0 {
				continue
			}
			//检查用户是否符合购买条件
			if g2.GoodsOptionId != 0 && g2.GoodsKind == 1 {
				mc := user.GetUserCollectByGoodsId(u.ID, g2.GoodsOptionId)
				if mc.ID == 0 {
					//执行退款操作
					err2 := common.Refound(v.OrderId)
					if err2 != nil {
						s := model.GetKey("refoundError")
						json.Unmarshal([]byte(s), &orderId)

						orderId = append(orderId, v.OrderId)

						b, _ := json.Marshal(orderId)

						model.SetKey("refoundError", string(b), 0)
					}
					continue
				}
			}

			if g2.GoodsCate == 3 {
				dr := goods.GetDrawRecordByUid(u.ID)
				if dr.ID == 0 {
					//执行退款操作
					err2 := common.Refound(v.OrderId)
					if err2 != nil {
						s := model.GetKey("refoundError")
						json.Unmarshal([]byte(s), &orderId)

						orderId = append(orderId, v.OrderId)

						b, _ := json.Marshal(orderId)

						model.SetKey("refoundError", string(b), 0)
					}
					continue
				}
			}

			//验证是否已经存在
			if user.CheckCollectExist(v.OrderId, u.ID) {
				continue
			}
			//通过验证进行入库
			user.Buildcollect(g2, u.ID, 1, v.OrderId, g2.GoodsKind, 1)
			//验证赠送盲盒
			sc := system.GetSysConfigDetail(fmt.Sprintf("goods_id_%d", g2.ID))

			if sc.ID > 0 {
				id_, _ := strconv.Atoi(sc.ConfigValue)
				gt := goods.GetGoodsDetail(id_)
				//通过验证进行入库
				user.Buildcollect(&gt, u.ID, 1, "", 2, 1)
			}
		}
	}
}

func (g GoodsSer) RetryRefount() {
	var orderId []string
	s := model.GetKey("refoundError")

	json.Unmarshal([]byte(s), &orderId)

	for _, v := range orderId {
		//执行退款操作
		common.Refound(v)
	}
}

func (g GoodsSer) AddRepoRecord(param common.Repo) error {
	return goods.AddRepoRecord(param)
}
